<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>水平垂直居中</title>
    <style>
        .app {
            display: grid;
            grid-template-columns: repeat(auto-fill, 120px);
        }

        .parent {
            height: 100px;
            width: 100px;
            background: skyblue;
        }

        .child {
            height: 60px;
            width: 60px;
            background: orange;
        }

        .use-flex {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .use-grid {
            display: grid;
            align-items: center;
            justify-content: center;
        }

        .use-absolute {
            position: relative;
        }

        .use-absolute .child {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .use-margin {
            display: flow-root;
            position: relative;
        }

        .use-margin .child {
            margin: auto;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
        }
    </style>
</head>

<body>
    <div class="app">
        <div class="parent use-flex">
            <div class="child"></div>
        </div>

        <div class="parent use-grid">
            <div class="child"></div>
        </div>

        <div class="parent use-absolute">
            <div class="child"></div>
        </div>

        <div class="parent use-margin">
            <div class="child"></div>
        </div>
    </div>
</body>

</html>